/*******************************************************************************
 * @project: aohuproject
 * @package: com.aohua.px
 * @file: VersionAutoPromotionAction.java
 * @author: fionn
 * @created: 2015年8月13日
 * @purpose:
 * 
 * @version: 1.0
 * 
 * 
 * Copyright 2015 HAND All rights reserved.
 ******************************************************************************/
package com.aohua.px;

import com.agile.api.APIException;
import com.agile.api.ChangeConstants;
import com.agile.api.IAgileSession;
import com.agile.api.IChange;
import com.agile.api.IDataObject;
import com.agile.api.INode;
import com.agile.api.IRow;
import com.agile.api.ITable;
import com.agile.api.ITwoWayIterator;
import com.agile.px.ActionResult;
import com.agile.px.EventActionResult;
import com.agile.px.ICustomAction;
import com.agile.px.IEventAction;
import com.agile.px.IEventInfo;
import com.agile.px.IWFChangeStatusEventInfo;

/**
 * @author fionn
 *
 */
public class VersionAutoPromotionAction implements ICustomAction, IEventAction {

    private static String version_default = "0.0";
    private static float version_step = 1.0f;
    
    /* (non-Javadoc)
     * @see com.agile.px.IEventAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.px.IEventInfo)
     */
    @Override
    public EventActionResult doAction(IAgileSession session, INode arg1,
            IEventInfo arg2) {
        EventActionResult result = null;
        try {
            session.disableAllWarnings();
            IWFChangeStatusEventInfo eventInfo = (IWFChangeStatusEventInfo)arg2;
            IChange change = (IChange)eventInfo.getDataObject();
            promotionVersion(change);
            result = new EventActionResult(arg2, new ActionResult(ActionResult.STRING, "Auto Version Promotion Action success."));
        } catch (Exception e) {
            result = new EventActionResult(arg2, new ActionResult(ActionResult.EXCEPTION, e));
            e.printStackTrace(System.out);
        } finally {
            try {
                session.enableAllWarnings();
            } catch (APIException e) {
                e.printStackTrace(System.out);
            }
        }
        
        return result;
    }

    /* (non-Javadoc)
     * @see com.agile.px.ICustomAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.api.IDataObject)
     */
    @Override
    public ActionResult doAction(IAgileSession session, INode arg1,
            IDataObject arg2) {
        ActionResult result = null;
        try {
            session.disableAllWarnings();
            IChange change = (IChange)arg2;
            promotionVersion(change);
            result = new ActionResult(ActionResult.STRING, "Auto Version Promotion Action success.");
        } catch (Exception e) {
            result = new ActionResult(ActionResult.EXCEPTION, e);
            e.printStackTrace(System.out);
        }finally {
            try {
                session.enableAllWarnings();
            } catch (APIException e) {
                e.printStackTrace(System.out);
            }
        }
        return result;
    }
    
    private void promotionVersion(IChange eco) throws APIException {
        ITable aitable = eco.getTable(ChangeConstants.TABLE_AFFECTEDITEMS);
        ITwoWayIterator it = aitable.getTableIterator();
        IRow row = null;
        while(it.hasNext()) {
            row = (IRow)it.next();
            String newVersion = getNewVersion(row);
            row.setValue(ChangeConstants.ATT_AFFECTED_ITEMS_NEW_REV, newVersion);
        }
    }
    
    private String getNewVersion(IRow row) throws APIException {
        Object obj = row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_OLD_REV);
        if(obj == null || "".equals(obj)) {
            return version_default;
        }
        try {
            float nver = Float.parseFloat(obj.toString()) + version_step;
            nver = Math.round(nver*10f)/10f;
            String newVersion = String.valueOf(nver);
            return newVersion;
        } catch (NumberFormatException e) {
            e.printStackTrace(System.out);
            return "";
        }
    }

}
